package hnlg.com.cn.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import hnlg.com.cn.service.HouseService;
import hnlg.com.cn.vo.House;
import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
@RequestMapping("/HouseControl")
public class HouseController {
	List<House> list=null;
	   @Resource HouseService houseService;
	   
	   @RequestMapping("/addHouse")
	   //若不加responsebody则被模板引擎解析为页面跳转
	   @ResponseBody
	   public void addLocation(House house) throws Exception{
		   log.info("开始增加house");
		   boolean flag=houseService.addHouse(house);
		   if(flag==true){
			   log.info("增加成功");
		   }else{
			   log.info("增加失败");
		   }
	   }
		
	   @GetMapping("/deleteHouse")
	   @ResponseBody
	   public void deleteHouse(@RequestParam("id")int id){
		   log.info("开始删除House");
		   try {
			houseService.deleteHouse(id);
			log.info("删除成功");
		} catch (Exception e) {
			log.info("删除失败");
			e.printStackTrace();
		 }
	   }
	   @RequestMapping("/updateHouse")
	   @ResponseBody
	   public void updateHouse(House house){
		   log.info("更新house");
		   try {
			houseService.updateHouse(house);
			log.info("更新成功");
		} catch (Exception e) {
		    log.info("更新失败");
			e.printStackTrace();
		}
		     
	   }
	   
	   @RequestMapping("/deleteBatch")
	   @ResponseBody
	   public void deleteBatch(@RequestParam("ck") String checkbox[]){
		   log.info("开始批量删除---");
		   try{
			   for(String obj:checkbox){
				   System.out.println("checkbox="+obj);
			   }
			   this.houseService.deleteBatch(checkbox);
			   log.info("批量删除成功");
		   }catch(Exception e){
			   log.info("批量删除失败");
			   e.printStackTrace();
		   }
		   
	   }
	   
	   @GetMapping("/queryHouse")
	   @ResponseBody
	   public List<House> queryHouse(@RequestParam(required=false,defaultValue="")String hid){
		   log.info("开始查询Location");
		   try{
			   House house = new House();
			   house.setHid(hid);;
			   list=houseService.queryHouse(house);
		   }catch(Exception e){
			   log.info("查询失败");
			   e.printStackTrace();
		   }
		   
		   return list;
	   }
}
